# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=markdown
_pyname=Markdown
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.1.1
pkgrel=2
pkgdesc="Python implementation of John Gruber's Markdown (mingw-w64)"
url='https://pypi.python.org/pypi/Markdown'
license=('BSD')
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-pbr"
             "${MINGW_PACKAGE_PREFIX}-python3-pbr"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-yaml"
              "${MINGW_PACKAGE_PREFIX}-python3-yaml")
source=("https://pypi.io/packages/source/${_pyname:0:1}/${_pyname}/${_pyname}-${pkgver}.tar.gz")
sha256sums=('2e50876bcdd74517e7b71f3e7a76102050edec255b3983403f1a63e7c8a41e7a')

prepare() {
  cd "${srcdir}"

  # bug in 2.4, some DOS line endings slipped in
  find "${_realname}-${pkgver}" -name '*py' -exec sed -i 's|\r||g' {} +

  # ImportError: No module named pkg_resources
  sed -i 's/^from pkg_resources.*$/import packaging.version/' "${_realname}-${pkgver}/markdown/__init__.py"

  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
  find "$srcdir/python2-build-${CARCH}" -name '*py' -exec sed -i 's|#!/usr/bin/env python$|&2|' {} +
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done 
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} -m unittest discover tests
  done
}


package_python3-markdown() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd ${srcdir}/python3-build-${CARCH}
  ${MINGW_PREFIX}/bin/python3 setup.py build
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|$(cygpath -wm ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${_f}"
  done

  install -D -m644 LICENSE.md "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-${_realname}/LICENSE.md
}

package_python2-markdown() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd ${srcdir}/python2-build-${CARCH}
  ${MINGW_PREFIX}/bin/python2 setup.py build
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|$(cygpath -wm ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${_f}"
  done

  for f in markdown_py; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done

  install -D -m644 LICENSE.md "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/LICENSE.md
}

package_mingw-w64-i686-python2-markdown() {
  install=python2-${_realname}-${CARCH}.install
  package_python2-markdown
}

package_mingw-w64-i686-python3-markdown() {
  install=python3-${_realname}-${CARCH}.install
  package_python3-markdown
}

package_mingw-w64-x86_64-python2-markdown() {
  install=python2-${_realname}-${CARCH}.install
  package_python2-markdown
}

package_mingw-w64-x86_64-python3-markdown() {
  install=python3-${_realname}-${CARCH}.install
  package_python3-markdown
}

check_python3-markdown() {
  [[ $(${MINGW_PREFIX}/bin/python3 -c "import markdown; print(markdown.version)") == "$pkgver" ]]
  [[ $(${MINGW_PREFIX}/bin/python3 -c "import markdown; print(markdown.markdown('*test*'))") == "<p><em>test</em></p>" ]]
}

check_python2-markdown() {
  [[ $(${MINGW_PREFIX}/bin/python2 -c "import markdown; print(markdown.version)") == "$pkgver" ]]
  [[ $(${MINGW_PREFIX}/bin/python2 -c "import markdown; print(markdown.markdown('*test*'))") == "<p><em>test</em></p>" ]]
}

check_mingw-w64-i686-python2-markdown() {
  check_python2-markdown
}

check_pmingw-w64-x86_64-python2-markdown() {
  check_python2-markdown
}

check_mingw-w64-i686-python3-markdown() {
  check_python3-markdown
}

check_pmingw-w64-x86_64-python3-markdown() {
  check_python3-markdown
}

